#include "kisql.h"
#include "/usr/include/mysql/mysql.h"

static char *server_args[] = {"this_program", "--datadir=.", "--key_buffer_size=32M" };
static char *server_groups[] = {"embeded", "server", "this_program_SERVER",(char *)NULL};

/* init & connect mysql */
int kisql_init(MYSQL *mysql)
{

    //init mysql_lib
    if(mysql_library_init(sizeof(server_args)/sizeof(char *), server_args, server_groups))
        return -1;

    puts("mysql_library_init ok...");

    mysql_init(mysql);
    if (!mysql)
    {
        perror("mysql init err");
        return -2;
    }
    puts("mysql init ok...");

    if(!mysql_real_connect(mysql, LOCAL_HOST, "root", "123456", "kisql", 3306, NULL, 0))
    {
        fprintf(stderr, "cant connect database , errnum: %s\n", mysql_error(mysql));
    }
    printf("connect kisql success ...\n");

    //mysql_close(mysql);
    // mysql_library_end();
    return 0;
}

/* safe close mysql */
void kisql_close(MYSQL *mysql)
{
    mysql_close(mysql);
    mysql_library_end();
}

/* send query cmd */
int kisql_query(MYSQL *mysql, const char *cmd)
{
    if(mysql_real_query(mysql,cmd, strlen(cmd)))
    {
        fprintf(stderr, "cant send cmd , errnum: %s\n", mysql_error(mysql));
        return -1;
    }
    return 0;
}

my_ulonglong kisql_affected_rows(MYSQL *mysql)
{
    printf("%ld records modified!",(long)mysql_affected_rows(mysql));
}

//function
int kisql_create_database(MYSQL *mysql, const char *database_name)
{
    char cmd[1024] = {0};
    sprintf(cmd, "CREATE DATABASE %s",database_name);
    return kisql_query(mysql, cmd);
}

int kisql_delete_database(MYSQL *mysql, const char *database_name)
{
    char cmd[1024] = {0};
    sprintf(cmd, "drop database %s",database_name);
    return kisql_query(mysql, cmd);
}

int kisql_change_db(MYSQL *mysql, const char *db_name)
{
    return mysql_select_db(mysql,db_name);
}

int kisql_create_table(MYSQL *mysql, const char *arg)
{
    char cmd[1024] = {0};
    sprintf(cmd, "CREATE TABLE %s",arg);
    return kisql_query(mysql, cmd);
}

int kisql_select_table(MYSQL *mysql, const char *args)
{
    MYSQL_RES* res;
    MYSQL_ROW row;
    int t;

    char cmd[1024] = {0};
    sprintf(cmd, "%s", args);
    t = kisql_query(mysql, cmd);
    if(t)
    {
        printf("select datebase err: %s\n", mysql_error(mysql));
    }
    else
    {
        res = mysql_store_result(mysql);
        while(row = mysql_fetch_row(res))
        {   
            printf("|");
            for(t = 0; t< mysql_num_fields(res); t++)
            {
                printf("\t%s|",row[t]);
            }
            printf("\n");
        }
        mysql_free_result(res);
    }
}

int kisql_insert_table(MYSQL *mysql, const char *table, const char *args)
{
    char cmd[1024] = {0};
    sprintf(cmd, "INSERT INTO %s VALUES %s",table, args);
    return kisql_query(mysql, cmd);

}

/* UPDATE table SET args WHERE condition */
int kisql_update_table(MYSQL *mysql, const char *table, const char *args, const char *condition)
{
    char cmd[1024] = {0};
    sprintf(cmd, "UPDATE %s SET %s WHERE %s",table, args, condition);
    return kisql_query(mysql, cmd);
}

int kisql_delete_table(MYSQL *mysql, const char *table, const char *condition)
{
    char cmd[1024] = {0};
    sprintf(cmd, "DELETE FROM %s WHERE %s",table, condition);
    return kisql_query(mysql, cmd);
}